#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"
void StackInit(Stack* pst)
{
	pst->data = NULL;
	pst->size = 0;
	pst->capacity = 0;
}

void StackPush(Stack* pst, STDataType x)
{
	if (pst->size == pst->capacity)
	{
		int newcapacity = pst->data == NULL ? 4 : pst->capacity * 4;
		STDataType* ptr = (STDataType*)realloc(pst->data, sizeof(STDataType) * newcapacity);
		if (ptr == NULL)
		{
			perror("malloc fail:");
			return;
		}
		pst->data = ptr;
		pst->capacity = newcapacity;
	}

	pst->data[pst->size] = x;
	pst->size++;

}

STDataType StackTop(Stack* pst)
{
	assert(!StackEmpty(pst));
	return pst->data[pst->size - 1];
}

void StackPop(Stack* pst)
{
	assert(!StackEmpty(pst));
	pst->size--;
}

void StackDestroy(Stack* pst)
{
	free(pst->data);
	pst->data = NULL;
	pst->size = pst->capacity = 0;
}

bool StackEmpty(Stack* pst)
{
	return pst->size == 0;
}

int StackSize(Stack* pst)
{
	return pst->size;
}